package com.gitee.wsl.common.ui.graphics.color.decoder

import androidx.compose.ui.graphics.Color
import com.gitee.wsl.common.ui.graphics.color.HEXAColor
import com.gitee.wsl.common.ui.graphics.color.toColor

/**
 * Decodes a [Color] from a hexadecimal string (e.g. `#FF0000`).
 */
object HexColorDecoder : ColorDecoder {
    override fun decode(raw: String): Color? {
        if (raw.firstOrNull() != '#') return null
        // Converted by Colormath.
        return try {
            HEXAColor.parseStringColor(raw).toColor()
        } catch (e: IllegalArgumentException) {
            null
        }
    }
}
